home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Text / Doc Makers / WEBSs / WEBSs Books / Tutorial / Texts / Scripts Examples < prev   
Encoding:
Text File  |  1993-05-18  |  7.3 KB  |  202 lines  |  [TEXT/WEBS]

  1. You can use the Copy/Paste commands from the Edit menu to copy the contents of the scripts shown below into the script browser.
  2.  
  3. 1   Assigning a Predefined Window Location to a Document
  4.  
  5. This script assigns to the current document the same window location as the document GraphTemplate.
  6.  
  7. ON Open OF ANY Document in [GraphDocs];
  8. VAR left,top,right,bottom: Integer;
  9. BEGIN
  10.    [GraphTemplate].GetWindowLoc(left,top,right,bottom);
  11.    current.SetWindowLoc(left,top,right,bottom); 
  12. END
  13.  
  14.  
  15. 2   Creating a Set of Visited Documents
  16.  
  17. This script checks if the set Visited Document exists and puts the current document into it.
  18.  
  19. ON Open OF ANY Document;
  20. VAR aSet : Set;
  21. BEGIN
  22.    aSet := GetNamedObject('Visited Documents');
  23.    IF aSet = nullObject THEN
  24.    BEGIN
  25.       aSet := NewSet;
  26.       aSet.SetName('Visited Documents')
  27.    END;
  28.    aSet.AddObject(current)
  29. END
  30.  
  31.  
  32. 3   Suggesting a Partial Ordering on Document Opening
  33.  
  34. This script asks the user whether he/she wants to study the document SimpleExample before the current one.
  35.  
  36. ON Open OF Document [ComplexExample];
  37. BEGIN
  38.    IF NOT ([SimpleExample] IN [Visited Documents]) THEN
  39.       IF ReadOk('Study a simpler example first?') THEN
  40.       BEGIN
  41.          [SimpleExample].Open;
  42.          Abort
  43.        END
  44. END
  45.  
  46.  
  47. 4   Selectively Closing Documents
  48.  
  49. This script closes all the documents attached to the current document, unless they belong to the set KeepOpenDocs.
  50.  
  51. ON Close OF ANY Document IN [AnchorDocuments];
  52. VAR doc : Document;
  53. BEGIN
  54.    FOR EACH doc IN current.GetAttachedDocuments DO
  55.       IF NOT (doc IN [KeepOpenDocs]) THEN doc.Close;
  56. END
  57.  
  58.  
  59. 5    Formatting a Text Document
  60.  
  61. This script applies several formatting commands to the current selection in a textual document (i.e. to obtain the format of the title above). To use it, select the text to format, choose the Execute Other Script command from the Script menu and execute the script.
  62.  
  63. ON EXECUTE;
  64. BEGIN
  65.    DoMenu('Times');
  66.    DoMenu('18 Point');
  67.    DoMenu('Underline');
  68. END
  69.  
  70.  
  71. 6   Creating a Simple Browser
  72.  
  73. This script creates a browser in the same directory as the application, which contains all the graphical documents in the set Visited Documents whose name starts with “Example”.
  74.  
  75. ON Execute;
  76. VAR   doc1:   BrowserDocument;
  77.       doc2:   PictDocument;
  78. BEGIN
  79.    DoMenu('New Browser Document…');
  80.    doc1 := AsUNIVObject(frontDocument); { frontDocument is of class Document, so we have to make it compatible with the class of doc1, i.e. BrowserDocument. Since we just created a browser document, this should not cause any problems.}
  81.    FOR EACH doc2 IN [Visited Documents] DO
  82.       IF Pos('Example',doc2.GetName) = 1 THEN
  83.       BEGIN { "Example" was found at the beginning of the document name }
  84.          doc1.CreateNode(doc2.GetName);
  85.          doc1.AddDocument(doc2.GetName,doc2)
  86.       END;
  87.    SetDirectory(':');
  88.    doc1.Save('Example Graphic Documents')
  89. END
  90.  
  91.                   
  92. 7   Using a Guided Tour
  93.  
  94. This script activates a browser and opens the documents associated with its nodes, asking the user after each visited node whether he/she wants to continue. Note that we use the variable theBrowser to store a reference to the browser, instead of using each time the object specifier [Demo.Table] which would force the system to search repeatedly for the same document within all the objects in the open webs, a time-consuming task.
  95.  
  96. ON Execute;
  97. VAR   theBrowser: BrowserDocument;
  98.       continue:   Integer;
  99. BEGIN
  100.    theBrowser := [Demo.Table];
  101.    theBrowser.Activate;
  102.    theBrowser.DeselectAll;
  103.    continue := true;
  104.    WHILE continue AND IsAvailable('Next Node') DO 
  105.    BEGIN
  106.       DoMenu('Next Node');
  107.       continue := ReadOk('Press OK to continue, Cancel to abort.');
  108.    END
  109. END
  110.  
  111.  
  112. 8   Searching and Replacing Strings in a Document
  113.  
  114. This script will replace all occurrences of the string s1 in the document doc with s2. 
  115.  
  116. ON Execute(doc: TextDocument;s1,s2: String);
  117. VAR index: Integer;
  118. BEGIN
  119.    doc.Select;       { makes the document the active one }
  120.    doc.DeselectAll;  { puts the cursor at the beginning of the document }
  121.    index := doc.FindString(s1,false);
  122.    WHILE index > 0 DO
  123.    BEGIN
  124.       doc.Selectstring(index,Length(s1));
  125.       doc.Insertstring(s2);
  126.       index := doc.FindString(s1,false)
  127.    END
  128. END
  129.  
  130. A possible use of the above script (supposedly named ReplaceAll) would be:
  131.   
  132.    [ReplaceAll].Execute([myDocument],'word1','word2')
  133.  
  134.  
  135. 9   Creating a a Glossary
  136.  
  137. The following script automatically builds a glossary, by  creating links between a series of words and all their occurences in a set of documents.  
  138.  
  139. For each block of type Glossary in the glossary document doc, the script will :
  140.  
  141. 1. Select the block marker and get the string theString corresponding to the block extent;
  142.  
  143. 2. For each document textDoc in set theSet, it will (2) :
  144.     – open the document and put the cursor at the beginning,
  145.     – (2a) search for theString,
  146.     – look if there is a block marker on the left of the word,
  147.     – if yes, select the block marker (to avoid creating several blocks for the same word), 
  148.        if no, create a block and select its marker,
  149.     – issue a Start Link command,
  150.     – select the glossary document doc,
  151.     – issue a Complete Link command,
  152.     – give the new link a special name and type (users and other scripts can make use of this),
  153.     – select the document textDoc and repeat the above process from (2a) until theString cannot be found.
  154.  
  155. 3. Close all documents in set theSet.
  156.  
  157. ON Execute(doc: TextDocument;theSet:Set);
  158. VAR   index:      Integer;
  159.       textDoc:    TextDocument;
  160.       tBlock:     TextBlock;
  161.       theString:  String;
  162. BEGIN
  163.    doc.Open;
  164.    doc.Select;
  165.    FOR EACH tBlock OF TYPE Glossary IN doc.GetBlocks DO
  166.    BEGIN
  167. {1}   tBlock.SelectMarker;
  168.       theString := tBlock.GetExtentString;
  169. {2}   FOR EACH textDoc IN theSet DO
  170.       BEGIN
  171.          textDoc.Open;
  172.          textDoc.Select;
  173.          textDoc.DeselectAll; { Put the cursor at the beginning }
  174.          index := textDoc.FindString(theString,false);
  175.          WHILE index > 0 DO { While the string is found }
  176.          BEGIN { check if there is already a block to avoid creating duplicate blocks }
  177.             IF textDoc.StringAt(index-1,1) = '' THEN 
  178.                textDoc.SelectString(index-1,1)
  179.             ELSE BEGIN
  180.                textDoc.SelectString(index,Length(theString));
  181.                DoMenu('Create Block');
  182.                textDoc.SelectString(index,1);
  183.             END;
  184.             DoMenu('Start Link');
  185.             doc.Select;
  186.             DoMenu('Complete Link'); { Create the link }
  187.             lastLink.SetName('Glossary - ' + textDoc.GetName); 
  188.             lastLink.SetType(Glossary);
  189.             textDoc.Select;
  190.             textDoc.SelectString(index+Length(theString),0); { to advance the cursor, so that we don't find the same word again and again }
  191.             index := textDoc.FindString(theString,false);
  192.          END;
  193.       END
  194.    END;
  195. {3}FOR EACH textDoc IN theSet DO textDoc.Close { to close all the documents in theSet }
  196. END
  197.  
  198. To use this script, create a glossary (textual) document, create blocks for the entries in the glossary document (e.g. by selecting each word and choosing the "Create Block" command from the WEBSs menu), and give them the type Glossary (do not forget to create the type Glossary before trying to compile the script). Put all the documents to be searched in a set, and put the line below in a script to call the above script (supposedly named "CreateGlossary") :
  199.  
  200.    [CreateGlossary].Execute([Glossary Document],[theSet])
  201.  
  202.